﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;

namespace 时钟
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lab时钟.Text = System.DateTime.Now.ToString("yyyy年MM月dd HH时mm分ss秒");
        }

        private void textBox_小时_KeyPress(object sender, KeyPressEventArgs e)
        {
            int key = e.KeyChar;
            if ((key < '0' || key > '9') && (key != 8))
                e.Handled = true;
            //e.Handled = true时表示已经处理了事件（即不处理当前键盘事件）
        }

        private void textBox_分钟_KeyPress(object sender, KeyPressEventArgs e)
        {
            int key = e.KeyChar;
            if ((key < '0' || key > '9') && (key != 8))
                e.Handled = true;
        }

        private void textBox_秒_KeyPress(object sender, KeyPressEventArgs e)
        {
            int key = e.KeyChar;
            if ((key < '0' || key > '9') && (key != 8))
                e.Handled = true;
        }

        private DateTime dt;

        private void btnset闹钟_Click(object sender, EventArgs e)
        {
            dt = DateTime.Now;
            dt = dt.AddHours(Convert.ToDouble(textBox_小时.Text));
            dt = dt.AddMinutes(Convert.ToDouble(textBox_分钟.Text));
            dt = dt.AddSeconds(Convert.ToDouble(textBox_秒.Text));
            //MessageBox.Show(dt.ToString());
            Alarm_clock.Enabled = true;
        }

        private void Alarm_clock_Tick(object sender, EventArgs e)
        {            
            if  (dt.Year == DateTime.Now.Year
                && dt.Month == DateTime.Now.Month
                && dt.Day == DateTime.Now.Day 
                && dt.Hour == DateTime.Now.Hour
                && dt.Minute == DateTime.Now.Minute
                && dt.Second == DateTime.Now.Second)
            {
                playSoundFromResource();
            }
        }
        private void playSoundFromResource()
        {
            string egm = "../../Resources/audio.wav";//定义变量来存储音频路径
            SoundPlayer bgm = new SoundPlayer(egm);//控制声音播放
            bgm.Play();//开始播放音乐
        }

    }
}